/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "doc_minitutorial.h"

#include <QFile>
#include <QTextStream>

MiniTutorial::MiniTutorial(const QString& title) : IntroducedDocument(title)
{
}

MiniTutorial::MiniTutorial(const QDomElement& de)
{
    fromDomElement(de);
}

MiniTutorial::~MiniTutorial()
{
    for(QList<Part*>::iterator it = parts.begin(); it != parts.end(); ++it)
		delete (*it);
}

QList<Part*>& MiniTutorial::allParts()
{
    return parts;
}

QDomElement MiniTutorial::toDomElement(QDomDocument& doc) const
{
    QDomElement minituto = doc.createElement("minituto");
    QDomElement deTitle = doc.createElement("title");
    QDomElement deLicence = doc.createElement("licence");
    QDomElement deProgress = doc.createElement("progress");
    QDomElement deDifficult = doc.createElement("difficult");
    QDomElement deIntro = doc.createElement("intro");
    QDomElement deParts = doc.createElement("parts");
    QDomElement deConclu = doc.createElement("conclu");

    deTitle.appendChild(doc.createCDATASection(title));
    deLicence.appendChild(doc.createCDATASection(QString::number(licence)));
    deProgress.appendChild(doc.createCDATASection(QString::number(progress)));
    deDifficult.appendChild(doc.createCDATASection(QString::number(difficult)));
    deIntro.appendChild(doc.createCDATASection(intro->getContents() ));
    deConclu.appendChild(doc.createCDATASection(conclu->getContents() ));

    for(QList<Part*>::const_iterator it = parts.begin(); it != parts.end(); ++it)
	deParts.appendChild( (*it)->toDomElement(doc) );

    minituto.appendChild(deTitle);
    minituto.appendChild(deLicence);
    minituto.appendChild(deProgress);
    minituto.appendChild(deDifficult);
    minituto.appendChild(deIntro);
    minituto.appendChild(deParts);
    minituto.appendChild(deConclu);

    return minituto;

}

void MiniTutorial::toFile(const QString& fname)
{
    // XML
    QDomDocument doc("Minituto");
    QDomElement root = doc.createElement("document");
    QDomElement minituto = toDomElement(doc);

    root.setAttribute("type", "minituto");
    root.appendChild(minituto);

    doc.appendChild( root );

    // FILE
    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
		QTextStream ts( &file );
		ts << doc.toString();
		file.close();
	
		filename = fname;
    }
    else
		throw 401;
}

void MiniTutorial::fromDomElement(const QDomElement& de)
{
    QDomElement deTitle = de.firstChildElement();
    QDomElement deLicence = deTitle.nextSiblingElement("licence");
    QDomElement deProgress = deTitle.nextSiblingElement("progress");
    QDomElement deDifficult = deTitle.nextSiblingElement("difficult");
    QDomElement deIntro = deTitle.nextSiblingElement("intro");
    QDomElement deParts = deTitle.nextSiblingElement("parts");
    QDomElement deConclu = deTitle.nextSiblingElement("conclu");

    if(deTitle.tagName() == "title" && !deIntro.isNull() && 
       !deParts.isNull() && !deConclu.isNull() )
    {
		setTitle(deTitle.text());
		setIntro(deIntro.text());
                setConclu(deConclu.text());
                setLicence(deLicence.text().toInt());
                setProgress(deProgress.text().toInt());
                setDifficult(deDifficult.text().toInt());
	
		QDomNodeList listParts = deParts.childNodes();
		int nbParts = listParts.count();
		if(nbParts > 0)
			for(int i = 0; i < nbParts; ++i)
			parts.append(new Part(listParts.at(i).toElement()));

    }
    else
		throw 300;
}


void MiniTutorial::fromFile(const QString& fname)
{
 
    QDomDocument doc("Minituto");
    QFile file(fname);

    if( file.open(QIODevice::ReadOnly) )
    {
		if( doc.setContent( &file ) )
		{
			file.close();
			
			QDomElement root = doc.documentElement();
			
			if(root.tagName() != "document")
				throw 300;
			
			if(root.firstChildElement().tagName() != "minituto")
				throw 300;

			filename = fname;
			fromDomElement(root.firstChildElement());
		}
		else
		{
			file.close();
				throw 301;
		}
    }
    else
		throw 400;
}
